/**
 * Created by lx on 2017/9/25.
 */
angular.module('module.table.operation')
    .controller('TableNumDetailController', TableNumDetailController);
TableNumDetailController.$inject = ['$state', '$stateParams', 'TableNumService', '$localStorage', 'TableGroupService', 'EquipmentService'];
function TableNumDetailController($state, $stateParams, TableNumService, $localStorage, TableGroupService, EquipmentService) {

    var vm = this;

    if (!$localStorage[environmental + "shopid"]) {
        $localStorage.$reset();
        $state.go('login');
    }

    $('.modal-backdrop ').hide(); // 初始化
    var tableid = $stateParams.tableid;
    var shopid = $localStorage[environmental + "shopid"];
    vm.openModal = openModal; // 解绑
    vm.getDevDetail = getDevDetail; // 获取详情
    vm.getDevInline = getDevInline; // 获取在线状态
    vm.changeModal = changeModal; // 换绑
    vm.openDeleteTable = openDeleteTable; // 打开删除桌台
    vm.checkTitle = checkTitle; // 显示修改桌台名
    vm.removeTitle = removeTitle; // 修改桌台名
    vm.sureBtn = sureBtn; // 模态框
    vm.cancelBtn = cancelBtn; // 模态框
    vm.searchDev = searchDev; // 查找设备
    vm.modifytitle = true;
    var modelId1, modelId2, modelDev;
    var tabledata;

    $(".modal-backdrop").remove();

    // 获取桌台详情
    TableNumService.getTableNum(tableid).$promise.then(function (sccF) {
        tabledata = sccF;
        vm.tabledetail = sccF;
        getDevIdByTableId();
        if (vm.tabledetail.groupid == 0) {
            vm.tabledetail.groupname = '暂无分组';
        } else {

            // 获取桌台所属分组
            TableGroupService.getGroupByGroupId(sccF.groupid).$promise.then(function (success) {
                vm.tabledetail.groupname = success[0].groupname;
            }, function (error) {
                if(error.data.msg == "该分组id不存在"){
                    //
                    vm.tabledetail.groupid = 0;
                    TableNumService.saveTableDetail(shopid, vm.tabledetail, function (sucData) {
                        vm.tabledetail.groupname = '暂无分组';
                    }, function (errData) {
                    })
                }else{
                    vm.messageVisible = true;
                    vm.message = error.data.msg;
                }
            })
        }
    }, function (errF) {
    });

    function getDevIdByTableId() {
        TableNumService.getDetailByTableId(shopid, tableid).$promise.then(function (sucData) {
            vm.flagstatus = true;
            vm.devlist = sucData;
        }, function (error) {
            vm.devlist = [];
            vm.flagstatus = false;
        })
    }

    // 解绑设备
    function openModal(dev) {
        $('#exampleModal').modal('show');
        var id1 = "#cancelbtn", id2 = "#updatesure";
        domChange(id1, id2, true);
        modelId1 = id1;
        modelId2 = id2;
        modelDev = dev;
        vm.unBinding = unBinding;
    }

    // 解绑操作
    function unBinding() {
        domChange(modelId1, modelId2, false);
        modelDev.tableid = -1;
        TableNumService.shopDevRelation(modelDev, function (success) {
            for (var i = 0; i < vm.devlist.length; i++) {
                if (success.devid == vm.devlist[i].devid) {
                    vm.devlist.splice(i, 1);
                    $('#exampleModal').modal('hide');
                }
            }
        }, function (error) {
            vm.messageVisible = true;
            vm.message = "解绑失败";
            modelDev.tableid = tableid;
        })
    }

    // 换绑设备
    function changeModal(dev) {
        var id1 = "#canceltwo", id2 = "#suretwo";
        domChange(id1, id2, true);
        $('#changeModal').modal('show');
        modelId1 = id1;
        modelId2 = id2;
        modelDev = dev;
        vm.changeBinding = changeBinding;
    }

    function changeBinding() {
        domChange(modelId1, modelId2, false);
        modelDev.devid = vm.devid;
        modelDev.createtime = new Date().getTime();
        TableNumService.modifyDev(modelDev.tableid, modelDev.devid, modelDev, function (success) {
            $('#changeModal').modal('hide');
        }, function (error) {
            $('#changeModal').modal('hide');
            vm.messageVisible = true;
            vm.message = error.data.msg + "，请重新绑定！";
            setTimeout(function () {
                location.reload();
            }, 1000);
        })
    }

    // 获取设备的状态
    function getDevDetail(dev) {
        EquipmentService.getAllChargeStatusByDevId(dev.devid).$promise.then(function (succse) {
            dev.detail = succse.createstatus;
        }, function (error) {
        })
    }

    function getDevInline(dev) {
        EquipmentService.getElectricityStatus(dev.devid, shopid).$promise.then(function (succe) {
            dev.statusEletricity = succe.status;
        })
    }

    function openDeleteTable() {
        $("#deletetableModal").modal('show');

        // 删除桌台
        vm.deleteTable = deleteTable;
    }

    function deleteTable() {
        $("#deletebtn").attr("disabled", true);
        TableNumService.deleteTable(shopid, Number(tableid), function (sucD) {
            $("#deletebtn").attr("disabled", false);
            vm.messageVisible = true;
            vm.message = "删除成功！";
            setTimeout(function () {
                $state.go('tablenum');
            }, 1000);
        }, function (error) {
            $("#deletebtn").attr("disabled", false);
            $("#deletetableModal").modal('hide');
        })
    }

    function checkTitle() {
        vm.modifytitle = false;
    }

    function removeTitle(tablename) {
        vm.modifytitle = true;
        if (tablename) {
            tabledata.tablename = tablename;
            TableNumService.saveTableDetail(shopid, tabledata, function (sucF) {
            }, function (error) {
            })
        } else {
            vm.messageVisible = true;
            vm.message = "请输入桌台名！";
        }

    }

    function sureBtn() {
        vm.messageVisible = false;
    }

    function cancelBtn() {
        vm.messageVisible = false;
    }

    function searchDev(dev) {
        EquipmentService.getStatusByAlarm(dev.devid, true, false, false, function (sucF) {
            vm.messageVisible = true;
            vm.message = "消息下发成功";
        }, function () {
            vm.messageVisible = true;
            vm.message = "消息下发失败";
        })
    }

    // 页面样式变化
    function domChange(id1, id2, type) {
        var dom1 = $(id1);
        var dom2 = $(id2);
        if (type) {
            dom1.addClass("detail_btn_active");
            dom2.removeClass("detail_btn_active");
        } else {
            dom1.removeClass("detail_btn_active");
            dom2.addClass("detail_btn_active");
        }
    }
}